extends Node2D
@onready var init_position: Node2D = %初始点位

func _ready() -> void:
	EventBus.subscribe("to_des", on_to_des)
	## 场景进入时也重置技能冷却
	reset_all_skill_cooldowns()
	
func on_to_des(_name):
	if _name == "main_scene":
		Utils.player.global_position = init_position.global_position
		## 重置所有技能冷却
		reset_all_skill_cooldowns()

## 重置所有技能冷却
func reset_all_skill_cooldowns():
	## 获取UI中的技能按钮
	var ui = get_tree().get_first_node_in_group("ui")
	if ui:
		var detail = ui.get_node_or_null("detail")
		if detail:
			var skills = detail.skills
			if skills:
				for skill_button in skills.get_children():
					if skill_button is SkillButton:
						## 重置技能冷却
						reset_skill_cooldown(skill_button)

## 重置单个技能冷却
func reset_skill_cooldown(skill_button: SkillButton):
	skill_button.reset_cooldown()

## 计算有多少节点
func get_total_node_count_iter() -> int:
	var stack: Array[Node] = [get_tree().current_scene]
	var count := 0
	while stack:
		var n = stack.pop_back()	
		count += 1
		for child in n.get_children():
			stack.append(child)
	return count

## 计算有多少节点有_process或者_physics_process函数
func get_total_node_count_iter_has_process() -> int:
	var stack: Array[Node] = [get_tree().current_scene]
	var count := 0
	while stack:
		var n: Object = stack.pop_back()
		if n.has_method("_process") or n.has_method("_physics_process"):
			count += 1
		for child in n.get_children():
			stack.append(child)
	return count
